package com.cre_drt;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;

public class ActivityTimeout extends FragmentActivity {

	public static final long DISCONNECT_TIMEOUT = 1000*60*5; // 5 minutes
	public SessionManager session;

	@SuppressLint("HandlerLeak")
	private Handler disconnectHandler = new Handler() {
		public void handleMessage(Message msg) {
		}
	};

	private Runnable disconnectCallback = new Runnable() {
		@Override
		public void run() {
			session = new SessionManager(getApplicationContext());
			session.hardLogout();
			// Perform any required operation on disconnect
		}
	};

	public void resetDisconnectTimer() {
		disconnectHandler.removeCallbacks(disconnectCallback);
		disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
	}

	public void stopDisconnectTimer() {
		disconnectHandler.removeCallbacks(disconnectCallback);
	}

	@Override
	public void onUserInteraction() {
		resetDisconnectTimer();
	}

	@Override
	public void onResume() {
		super.onResume();
		resetDisconnectTimer();
	}

	@Override
	public void onStop() {
		super.onStop();
		stopDisconnectTimer();
	}
}